package net.zhangbaolin.systemadmin.config;

import net.zhangbaolin.systemadmin.handle.RestAuthenticationEntryPoint;
import net.zhangbaolin.systemadmin.handle.RestfulAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import net.zhangbaolin.systemadmin.filter.JwtAuthenticationTokenFilter;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    @Autowired
    RestAuthenticationEntryPoint restAuthenticationEntryPoint;

    @Autowired
    RestfulAccessDeniedHandler restfulAccessDeniedHandler;

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        //关闭csrf  cors
        http.csrf().disable()
                .cors().disable();
        // 允许对于网站静态资源的无授权访问
        http.authorizeHttpRequests(
                authorizeHttpRequests->authorizeHttpRequests
                        .antMatchers(HttpMethod.POST,"/login").permitAll()
                        .antMatchers("/getCaptcha").permitAll()
                        // 允许对于网站静态资源的无授权访问
                        .antMatchers(HttpMethod.GET,
                                "/",
                                "/*.html",
                                "/favicon.ico",
                                "/**/*.html",
                                "/**/*.css",
                                "/**/*.js"
                        ).permitAll()
                        .anyRequest().authenticated());

        //关闭默认表单
        http.formLogin().disable();

        //关闭session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //添加jwt自定义拦截器
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //http.addFilterBefore(jwtAuthenticationTokenFilter, UserDetailsServiceFilter.class);

        http.exceptionHandling()
                .authenticationEntryPoint(restAuthenticationEntryPoint)//未登录处理类
                .accessDeniedHandler(restfulAccessDeniedHandler);//权限不足处理类

       return http.build();
    }

    //设置密码加密方式
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
}

